package com.ehotting.edsta.circuitDesign.bean;

import com.ehotting.edsta.circuitDesign.bean.request.RequestCodeExecute;
import com.ehotting.edsta.circuitDesign.enums.ChannelTypeInputEnum;
import com.ehotting.edsta.circuitDesign.enums.ChannelTypeOutEnum;
import com.ehotting.edsta.circuitDesign.enums.DigitalTypeEnum;
import com.ehotting.edsta.circuitDesign.enums.SignalTypeEnum;
import com.ehotting.eaf.core.utils.StringUtils;
import org.apache.commons.lang3.SerializationUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 电路组
 */
public class CircuitGroupBean extends HashMap<Long, CircuitElementBean> {

    private static final Logger logger = LoggerFactory.getLogger(CircuitGroupBean.class);

    private SignalTypeEnum signalType;

    private String digitalInVolt = "0";

    public SignalTypeEnum getSignalType() {
        return signalType;
    }

    public void setSignalType(SignalTypeEnum signalType) {
        this.signalType = signalType;
    }

    public String getDigitalInVolt() {
        return digitalInVolt;
    }

    public void setDigitalInVolt(String digitalInVolt) {
        this.digitalInVolt = digitalInVolt;
    }

    public ChannelTypeInputEnum getChannelType(){
        CircuitElementBean  v = this.entrySet().stream().findFirst().get().getValue();
        return v.getChannelTypeInput();
    }

    public String getIdAndCountKey(){
        StringBuffer buf = new StringBuffer();
        Set<Long> set = this.keySet().stream().sorted().collect(Collectors.toSet());
        for(Long l : set){
            buf.append(l).append("-").append(this.get(l).getCount()).append(";");
        }
        return buf.toString();


    }

    //todo 设置传感电路组key ok
    public String getCircuitGroupKey(){
        String chCode = this.getChannelType().getCode();
        String siCode = this.getSignalType().getCode();
        String inCode = this.getDigitalInVolt();
        return  chCode+"_"+ siCode+"_"+inCode;
    }

    public void putAll(HashMap <Long, CircuitElementBean> m) {
        Set<Long> set = m.keySet();
        for(Long key : set){
            if(this.containsKey(key)){
                CircuitElementBean c1 = this.get(key).clone();
                CircuitElementBean c2 =  m.get(key).clone();
                for(RequestCodeExecute.InputChannel ct : c2.getChannelList()){
                    if(!c1.getChannelList().contains(ct)){
                        c1.getChannelList().add(ct);
                    }
                }
                c1.setCount((int)Math.ceil(1.0 * c1.getChannelList().size()/c1.getChannelNum()));
                this.put(key,c1);
            }else{
                this.put(key,m.get(key).clone());

            }
        }
    }


    /***
     * 总通道数量
     * @return
     */
    public Integer getAllChannelCount(){
        Set<Long> set = this.keySet();
        Integer count = 0;
        for(Long key : set){
            CircuitElementBean c1 = this.get(key);
            count += c1.getCount()*c1.getChannelNum();
        }
        return count;
    }

    /***
     * 模拟输出通道数量
     * @return
     */
    public Integer getAllOutChannelCount4Analogue(){
        Integer count = 0;
        for(Long key : this.keySet()){
            CircuitElementBean value = this.get(key);
            if(value.getChannelTypeOut() == ChannelTypeOutEnum.ANALOGUE){
                count += value.getCount()*value.getChannelNum();
            }
        }
        return count;
    }


    /***
     * 数字输出通道数
     * @return
     */
    public Integer getAllOutChannelCount4Digital(){
        Integer count = 0;
        for(Long key : this.keySet()){
            CircuitElementBean value = this.get(key);
            if(value.getChannelTypeOut() == ChannelTypeOutEnum.DIGITAL){
                count += value.getCount()*value.getChannelNum();
            }
        }
        return count;
    }

    // 电路组最大采样率
    public Integer getMaxSample(){
        int maxSample = 0;
        for(Long k : this.keySet()){
            CircuitElementBean elementBean = this.get(k);
            maxSample = elementBean.getSample()>maxSample?elementBean.getSample():maxSample;
        }
        return maxSample;
    }

    /***
     * 电路模块总统计
     * @return
     */
    public Integer getCircuitCount(){
        Set<Long> set = this.keySet();
        Integer count = 0;
        for(Long key : set){
            CircuitElementBean c1 = this.get(key);
            count += c1.getCount();
        }
        return count;
    }

    @Override
    public CircuitGroupBean clone() {
        return (CircuitGroupBean) SerializationUtils.clone(this);
    }


    @Override
    public CircuitElementBean put(Long key,CircuitElementBean circuitElementBean){
        if(this.containsKey(key)){
            this.get(circuitElementBean.getCircuit().getId()).add();
        }
        super.put(key,circuitElementBean.clone());
        return circuitElementBean;
    }


    public List<CircuitElementBean> getCircuitElementList(){
        List<CircuitElementBean> circuitElementList = new ArrayList<>();
        Set<Map.Entry<Long, CircuitElementBean>> entrySet = this.entrySet();
        for(Map.Entry<Long, CircuitElementBean> entry: entrySet){
            circuitElementList.add(entry.getValue());
        }
        return circuitElementList;
    }

    @Override
    public boolean equals(Object object){
        CircuitGroupBean o = (CircuitGroupBean) object;
        Set<Long> oKeySet =  o.keySet();
        Set<Long> keySet =  this.keySet();
        return keySet.equals(oKeySet);
    }

}
